// xc_lshop/integralmall/my/exchange.js
var actbill = require('../model/integralorder_bill')
const app = getApp()

Page({
  mixins: [require('../../../xc/mixins/basicMixin')],

  /**
   * 页面的初始数据
   */
  data: {
    activetab: "0",
    CustomBar: app.globalData.CustomBar,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  onnavChange(e) {
    var that = this;
    this.setData({
      activetab: e.detail.name
    })
    that.getpagelist(that, 1)
  },
  xcloaddata: function (options, config) {
    console.log("config")
    console.log(config)
    if (config.integral) {
      this.setData({
        integralconfig: config.integral
      })
    }
    if (config.delivery_config) {
      this.setData({
        delivconfig: config.delivery_config
      })
    }
    if (options.opstatus) {
      var update = {};
      if (options.opstatus > 10) {
        update['navlist'] = this.data.refundnav;
      }
      update['activetab'] = options.opstatus;
      this.setData(update)
    }
    if (config.prothem && config.prothem.themcolor) {
      var themconfig = config.prothem.themcolor.config;
      this.setData({
        themconfig: themconfig
      })
    }
    var strwhere = {}
    // strwhere["status"] = 1
    var app = getApp();
    var that = this;
    app.xc_sys_config.authop.oppower("login").then(function (res) {
      if (res.status == 1) {
        that.getpagelist(that, 1, strwhere)
      }
    })
  },
  optool(ev) {
    var op = ev.target.dataset.op
    var dataset = ev.currentTarget.dataset
    var item = dataset.item
    // console.log("fff111")

    var that = this;
    switch (op) {
      case "qrcode": {
        this.set(item.status, item.ordernum)
        // console.log("fff")
        //  this.selectComponent("#qrcode").set(item.status,item.ordernum) ;
        break;
      }
      case "buyagain": {
        actcart.buyagain(item.orderinfo)
        wx.redirectTo({
          url: '../cart/index',
        })
        //cart.buyagain
        break;
      }
      case "payoldorder": {
        this.opload(true, "支付中...")
        actbill.payoldorder(item.id).then(item => {
          console.log(item)
          if (item["op"] == "paysuccess") {
            that.onChangetabs({
              detail: {
                name: "1"
              }
            }, true)
          } else if (item["op"] == "sendoder") {
            if (item['data']['status'] == -1) {
              wx.showModal({
                title: '错误',
                content: item['data']["message"],
              })
            }else if(item['data']['status']==-2){
              wx.showModal({
                title: '错误',
                content: item['data']["message"],
              })
            }
          }
          this.opload(false)
        })
        break;
      }
      case "closedorder": {
        wx.showModal({
          title: '确认关闭订单',
          content: "确认关闭订单",
          success(res) {
            if (res.confirm) {
              that.opload(true, "关闭订单中")
              actbill.closedorder(item.id).then(item => {
                if (item["status"] == 1) {
                  that.opload(false)
                  that.getpagelist(that, 1)
                  wx.showToast({
                    title: "操作成功"
                  })
                } else {
                  wx.showToast({
                    title: "操作失败"
                  })
                }

              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        break;
      }
      case "finishbill": {
        var that = this;
        wx.showModal({
          title: '确认收货',
          content: "我确认，已收到货",
          success(res) {
            if (res.confirm) {
              actbill.finishbill(item.id).then(item => {
                if (item["status"] == 1) {
                  that.getpagelist(that, 1, {})
                  // this.setData（{
                  //   xc
                  // }
                  // that.onChangetabs({
                  //   detail: {
                  //     name: "9"
                  //   }
                  // }, true)
                  // that.opload(true)
                } else {
                  wx.showToast({
                    title: "操作失败"
                  })
                }

              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        break;
      }
      case "applyrefund": {
        var Comapplyrefund = this.selectComponent("#applyrefund");
        Comapplyrefund.showmodel(item.ordernum, 'order')
        break;
      }
      case "comment": {
        var Comordercomment = this.selectComponent("#ordercomment");
        Comordercomment.showmodel(item)
        break;
      }
    }
  },
  onChangetabs: function (ev, isupdate) {
    if (isupdate) {
      // console.log(ev.detail.name)

      this.setData({
        activetab: ev.detail.name
      })
    } else {
      this.data.activetab = ev.detail.name
    }
    var that = this;
    that.getpagelist(that, 1, {})
  },
  getpagelist(that, rest, strwhere) {
    if (rest == 1) {
      if (!strwhere) {
        strwhere = {}
      }
      strwhere["status"] = this.data.activetab
      that.setData({
        modellist: [],
        isempty: false
      });
    }
    actbill.offset(that, rest, strwhere).then(function (result) {
      var updatemdoel = {};
      if ((result["code"] == "over" && result["obj"]) || result["code"] == "loadmore") {
        if (result["obj"]["modellist"].length < 1) {
          updatemdoel["isempty"] = true
        } else if (rest == 1) {
          updatemdoel["modellist"] = result["obj"]["modellist"];
        } else {
          if (result["obj"]["modellist"].length > 0) {
            updatemdoel["modellist"] = that.data.modellist.concat(result["obj"]["modellist"]);
          }
        }
      }
      // that.selectComponent("#ordercomment").showmodel( updatemdoel["modellist"][0],"order")      
      wx.stopPullDownRefresh();
      that.setData(updatemdoel);
    }, function () {

    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    that.getpagelist(that, -1)
  },

  
})